﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using AGVController.AgvModel;
using AGVController.BaseClass;
using Newtonsoft.Json;
using UYD_WCS_AS_AGV_Controller.BaseClass;

namespace UYD_WCS_AS_AGV_Controller
{
    public class AgvHelper
    {
        public string AgvIP { get; set; }
        public bool AgvConneed { get; set; }
        public AGVController.ServiceAgvStatus AgvStatusApi { get; set; }
        public AGVController.ServiceAgvControl AgvControlApi { get; set; }
        public AGVController.ServiceAgvNavigate AgvNavigateApi { get; set; }
        public AGVController.ServiceAgvSetting AgvSettingApi { get; set; }
        public AGVController.ServiceAgvOther AgvOtherApi { get; set; }
        public AGVController.ServiceAgvPush AgvPushApi { get; set; }
        public AgvHelper(string agvIp)
        {
            AgvIP = agvIp;
            AgvStatusApi = new AGVController.ServiceAgvStatus(AgvIP, 19204);
            AgvControlApi = new AGVController.ServiceAgvControl(AgvIP, 19205);
            AgvNavigateApi = new AGVController.ServiceAgvNavigate(AgvIP, 19206);
            AgvSettingApi = new AGVController.ServiceAgvSetting(AgvIP, 19207);
            AgvOtherApi = new AGVController.ServiceAgvOther(AgvIP, 19210);
            AgvPushApi = new AGVController.ServiceAgvPush(AgvIP, 19301);

            AgvConneed = true;
        }

        #region AGV指令下发


        /// <summary>
        /// 发送移动任务
        /// </summary>
        /// <param name="source_id"></param>
        /// <param name="id"></param>
        /// <returns></returns>
        public robot_base_res addTask(string source_id, string id)
        {
            int iSendTaskCount = 1;
            robot_base_res retRes = new robot_base_res();
            while (iSendTaskCount <= 3)
            {
                Log.saveToLog(Log.locType.Agv, "[" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "]-给AGV发送第" + iSendTaskCount.ToString() + "次任务下发");

                try
                {
                    //查询定位状态

                    string strInPrarm = "";
                    Log.saveToLog(Log.locType.Agv, "[" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "]-给AGV发送查询机器人定位状态入参:" + strInPrarm);
                    string strOutPrarm = AgvStatusApi.robot_status_reloc_req("");
                    Log.saveToLog(Log.locType.Agv, "[" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "]-给AGV发送查询机器人定位状态出参:" + strOutPrarm);

                    robot_status_reloc_res relocRes = JsonConvert.DeserializeObject<robot_status_reloc_res>(strOutPrarm);
                    if (relocRes.ret_code > 0)
                    {
                        retRes.ret_code = relocRes.ret_code;
                        retRes.err_msg = relocRes.err_msg;

                        iSendTaskCount++;
                    }
                    else
                    {
                        if (relocRes.reloc_status == 3 || relocRes.reloc_status == 1)
                        {

                            //抢占控制权
                            robot_config_lock_req rclr = new robot_config_lock_req();
                            rclr.nick_name = "AgvHelper";
                            strInPrarm = JsonConvert.SerializeObject(rclr);
                            Log.saveToLog(Log.locType.Agv, "[" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "]-给AGV发送抢占控制权入参:" + strInPrarm);
                            strOutPrarm = AgvSettingApi.robot_config_lock_req(strInPrarm);
                            Log.saveToLog(Log.locType.Agv, "[" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "]-给AGV发送抢占控制权出参:" + strOutPrarm);
                            retRes = JsonConvert.DeserializeObject<robot_base_res>(strOutPrarm);
                            if (retRes.ret_code == 0)
                            {

                                ////确认定位正常
                                //Log.saveToLog(Log.locType.Agv, "[" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "]-给AGV发送确认定位正常入参:");
                                //strOutPrarm = AgvControlApi.robot_control_comfirmloc_req("");
                                //Log.saveToLog(Log.locType.Agv, "[" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "]-给AGV发送确认定位正常出参:"+strOutPrarm);
                                //robot_base_res resConfirmLocRes = JsonConvert.DeserializeObject<robot_base_res>(strOutPrarm);
                                //if (resConfirmLocRes.ret_code > 0)
                                //{
                                //    retRes.ret_code = resConfirmLocRes.ret_code;
                                //    retRes.err_msg = resConfirmLocRes.err_msg;

                                //    iSendTaskCount++;
                                //}
                                //else
                                //{
                                    robot_task_gotarget_req taskReq = new robot_task_gotarget_req();
                                    taskReq.source_id = source_id;
                                    taskReq.id = id;
                                    strInPrarm = JsonConvert.SerializeObject(taskReq);
                                    Log.saveToLog(Log.locType.Agv, "[" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "]-给AGV发送路径导航入参:" + strInPrarm);
                                    strOutPrarm = AgvNavigateApi.robot_task_gotarget_req(strInPrarm);
                                    Log.saveToLog(Log.locType.Agv, "[" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "]-给AGV发送路径导航出参:" + strOutPrarm);

                                    retRes = JsonConvert.DeserializeObject<robot_base_res>(strOutPrarm);
                                    if (retRes.ret_code == 0)
                                    {
                                        Log.saveToLog(Log.locType.Agv, "[" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "]-给AGV发送释放控制权入参:");
                                        //释放控制权
                                        strOutPrarm = AgvSettingApi.robot_config_unlock_req("");
                                        Log.saveToLog(Log.locType.Agv, "[" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "]-给AGV发送释放控制权出参:" + strOutPrarm);

                                        iSendTaskCount = 4;
                                    }
                                //}
                            }
                            else
                            {
                                iSendTaskCount++;
                            }
                        }
                        else
                        {
                            retRes.ret_code = 99998;
                            retRes.err_msg = "AGV定位状态=" + relocRes.reloc_status + ",请等待定位完成";
                            iSendTaskCount++;
                        }
                    }
                }
                catch (Exception exc)
                {
                    retRes.ret_code = 99999;
                    retRes.err_msg = "系统异常:" + exc.Message + exc.StackTrace;
                    iSendTaskCount++;
                }

            }
            return retRes;
        }

        /// <summary>
        /// 暂停当前导航
        /// </summary>
        /// <returns></returns>
        public robot_base_res pauseTask()
        {
            robot_base_res retRes = new robot_base_res();
            try
            {
                Log.saveToLog(Log.locType.Agv, "[" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "]-给AGV发送暂停当前导航入参:");
                string strOutPrarm = AgvNavigateApi.robot_task_pause_req("");
                Log.saveToLog(Log.locType.Agv, "[" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "]-给AGV发送暂停当前导航出参:" + strOutPrarm);
                retRes = JsonConvert.DeserializeObject<robot_base_res>(strOutPrarm);
            }
            catch (Exception exc)
            {
                retRes.ret_code = 99999;
                retRes.err_msg = "系统异常:" + exc.Message + exc.StackTrace;
            }
            return retRes;
        }
        /// <summary>
        /// 继续当前导航
        /// </summary>
        /// <returns></returns>
        public robot_base_res resumeTask()
        {
            robot_base_res retRes = new robot_base_res();
            try
            {
                Log.saveToLog(Log.locType.Agv, "[" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "]-给AGV发送继续当前导航入参:");
                string strOutPrarm = AgvNavigateApi.robot_task_resume_req("");
                Log.saveToLog(Log.locType.Agv, "[" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "]-给AGV发送继续当前导航出参:" + strOutPrarm);
                retRes = JsonConvert.DeserializeObject<robot_base_res>(strOutPrarm);
            }
            catch (Exception exc)
            {
                retRes.ret_code = 99999;
                retRes.err_msg = "系统异常:" + exc.Message;
            }
            return retRes;
        }
        /// <summary>
        /// 取消当前导航
        /// </summary>
        /// <returns></returns>
        public robot_base_res cancelTask()
        {
            robot_base_res retRes = new robot_base_res();
            try
            {
                Log.saveToLog(Log.locType.Agv, "[" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "]-给AGV发送取消当前导航入参:");
                string strOutPrarm = AgvNavigateApi.robot_task_cancel_req(""); 
                Log.saveToLog(Log.locType.Agv, "[" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "]-给AGV发送取消当前导航出参:" + strOutPrarm);

                retRes = JsonConvert.DeserializeObject<robot_base_res>(strOutPrarm);
            }
            catch (Exception exc)
            {
                retRes.ret_code = 99999;
                retRes.err_msg = "系统异常:" + exc.Message + exc.StackTrace;
            }
            return retRes;
        }
        /// <summary>
        /// 发送配置机器⼈推送端⼝
        /// </summary>
        /// <returns></returns>
        public robot_base_res ConfigPush()
        {
            robot_base_res retRes = new robot_base_res();
            try
            {
                if (robot_push.x > 0)
                {
                    retRes.ret_code = 99999;
                    retRes.err_msg = "当前已经设置为自动推送模式";
                }
                else
                {
                    robot_config_push_req rcpr = new robot_config_push_req();
                    rcpr.interval = 3000;
                    rcpr.included_fields = Common.Robot_Push_included_fields;
                    rcpr.persistent = true;
                    string strInPrarm = JsonConvert.SerializeObject(rcpr);
                    Log.saveToLog(Log.locType.Agv, "[" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "]-给AGV发送发送配置机器⼈推送端⼝入参:");
                    string strOutPrarm = AgvSettingApi.robot_config_push_req(strInPrarm);
                    Log.saveToLog(Log.locType.Agv, "[" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "]-给AGV发送发送配置机器⼈推送端⼝出参:"+strOutPrarm);

                    retRes = JsonConvert.DeserializeObject<robot_base_res>(strOutPrarm);
                }
            }
            catch(Exception exc)
            {
                retRes.ret_code = 99999;
                retRes.err_msg = exc.Message+exc.StackTrace;
            }
            return retRes;
        }

        /// <summary>
        /// 软急停
        /// </summary>
        /// <param name="flag">true 为输出急停, false 为不输出急停</param>
        /// <returns></returns>
        public robot_base_res robot_other_softemc_req(bool flag)
        {
            robot_base_res retRes = new robot_base_res();
            try
            {
                robot_other_softemc_req rcpr = new robot_other_softemc_req();
                rcpr.status = flag;
                string strInPrarm = JsonConvert.SerializeObject(rcpr);
                Log.saveToLog(Log.locType.Agv, "[" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "]-给AGV发送发送软急停入参:" + strInPrarm);
                string strOutPrarm = AgvOtherApi.robot_other_softemc_req(strInPrarm);
                Log.saveToLog(Log.locType.Agv, "[" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "]-给AGV发送发送软急停出参:" + strOutPrarm);
                retRes = JsonConvert.DeserializeObject<robot_base_res>(strOutPrarm);
            }
            catch (Exception exc)
            {
                retRes.ret_code = 99999;
                retRes.err_msg = exc.Message + exc.StackTrace;
            }
            return retRes;
        }


        /// <summary>
        /// AGV 定位确认
        /// </summary>
        /// <returns></returns>
        public robot_base_res robot_control_comfirmloc_req()
        {
            robot_base_res retRes = new robot_base_res();
            try
            {
                Log.saveToLog(Log.locType.Agv, "[" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "]-给AGV发送定位确认入参:");
                string strOutPrarm = AgvControlApi.robot_control_comfirmloc_req("");
                Log.saveToLog(Log.locType.Agv, "[" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "]-给AGV发送定位确认出参:" + strOutPrarm);
                retRes = JsonConvert.DeserializeObject<robot_base_res>(strOutPrarm);
            }
            catch (Exception exc)
            {
                retRes.ret_code = 99999;
                retRes.err_msg = "系统异常:" + exc.Message + exc.StackTrace;
            }
            return retRes;
        }


        #endregion

    }
}
